package helper.request.logandreg;

import helper.response.LoginResponse;
import server.dbconnector.DatabaseConnector;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import base.*;

public class LoginRequest implements helper.request.RequestBeforeLogin {
	private static final long serialVersionUID = 147129160718232810L;
	private String userName;
	private String password;
	private boolean isAdmin;// true for admin , otherwise common user
	private LoginResponse lr;

	public LoginRequest(String userName, String password, boolean isAdmin) {
		this.userName = userName;
		this.password = password;
		this.isAdmin = isAdmin;
	}

	@Override
	public void process(DatabaseConnector dc) throws SQLException {
		ResultSet rsuser = dc.query("select * from user where username='"
				+ userName + "' and password='" + password + "'");
		if (!rsuser.first()) {
			lr = new LoginResponse(null, null, null, null, null, null, false);
			return;
		} else if (rsuser.getInt(8) == 0) {
            lr=new LoginResponse(null,null,null,null,null,null,false);
            return;
		} else {
			User cur = new User(rsuser.getInt(1), rsuser.getString(2), rsuser
					.getString(3), rsuser.getString(4), rsuser.getInt(5),
					rsuser.getString(6), rsuser.getString(7), false);
			if (isAdmin && rsuser.getInt(9)==1) {
				cur.setAdmin(true);
				cur.setAdmin(true);
				lr = new LoginResponse(cur, null, null, null, null, null, true);
				return;
			}
			else if(isAdmin && rsuser.getInt(9)!=1){
				cur.setAdmin(false);
			}
			rsuser.close();
			ArrayList<Plan> plans = new ArrayList<Plan>();
			ArrayList<Contact> contacts = new ArrayList<Contact>();
			ArrayList<Msg> msgs = new ArrayList<Msg>();
			ArrayList<Log> logs = new ArrayList<Log>();
			ResultSet rsplan = dc.query("select * from plan where userid="
					+ cur.getid());
			if (rsplan.first()) {
				while (true) {
					try {
						String availstr = rsplan.getString(9);
						String[] avail = availstr.split(",");
						ArrayList<Integer> ids = new ArrayList<Integer>();
						for (int i = 0; i < avail.length && avail[i] != ""; i++)
							ids.add(Integer.parseInt(avail[i]));
						Plan current = new Plan(rsplan.getInt(1), rsplan
								.getString(3), rsplan.getString(4), rsplan
								.getInt(2), ids, rsplan.getInt(6), rsplan
								.getInt(7), rsplan.getInt(8), DateFormat
								.getDateInstance().parse(rsplan.getString(5)));
						plans.add(current);
					} catch (Exception e) {
						e.printStackTrace();
					}
					if (!rsplan.next())
						break;
				}
			}
			rsplan.close();
			ResultSet rscont = dc
					.query("select * from contact where (userIDFrom=0 or userIDFrom="
							+ cur.getid() + ") and approved=1");
			if (rscont.first()) {
				while (true) {
					Contact current = new Contact(rscont.getInt(1), rscont
							.getInt(2), rscont.getString(3), rscont.getInt(4),
							rscont.getString(5), rscont.getBoolean(6));
					contacts.add(current);
					if (!rscont.next())
						break;
				}
			}
			rscont.close();
			ResultSet rslog = dc.query("select * from log where userID="
					+ cur.getid());
			if (rslog.first()) {
				while (true) {
					try {
						Log currentlog = new Log(rslog.getInt(1), rslog
								.getString(2), DateFormat.getDateInstance()
								.parse(rslog.getString(3)));
						logs.add(currentlog);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					if (!rslog.next())
						break;
				}
			}
			rslog.close();
			ResultSet rsmsg = dc
					.query("select * from message where msgToUserID="
							+ cur.getid());
			if (rsmsg.first()) {
				while (true) {
					Msg curmsg = new Msg(rsmsg.getString(3), rsmsg.getInt(2),
							rsmsg.getInt(1), rsmsg.getBoolean(4));
					msgs.add(curmsg);
					if (!rsmsg.next())
						break;
				}
			}
			dc.commit("delete from message where msgToUserID="+cur.getid());
			rsmsg.close();
			ArrayList<ArrayList<Plan>> contplan = new ArrayList<ArrayList<Plan>>();
			for (int i = 0; i < contacts.size(); i++) {
				ArrayList<Plan> curlist = new ArrayList<Plan>();
				Contact currentcontact = contacts.get(i);
				ResultSet currs = dc.query("select * from user where ID="
						+ currentcontact.gettargetid());
				currs.first();
				User curu = new User(currs.getInt(1), currs.getString(2), currs
						.getString(3), currs.getString(4), currs.getInt(5),
						currs.getString(6), currs.getString(7), currs
								.getBoolean(10));
				currentcontact.settargetuser(curu);
				currs.close();
				ResultSet plan2rs = dc.query("select * from plan where userid="
						+ curu.getid());
				if (plan2rs.first()) {
					while (true) {
						try {
							String availstr = plan2rs.getString(9);
							String[] avail = availstr.split(",");
							ArrayList<Integer> ids = new ArrayList<Integer>();
							for (int j = 0; j < avail.length && avail[j] != ""; j++)
								ids.add(Integer.parseInt(avail[j]));
							Plan current = new Plan(plan2rs.getInt(1), plan2rs
									.getString(3), plan2rs.getString(4),
									plan2rs.getInt(2), ids, plan2rs.getInt(6),
									plan2rs.getInt(7), plan2rs.getInt(8),
									DateFormat.getDateInstance().parse(
											plan2rs.getString(5)));
							curlist.add(current);
						} catch (Exception e) {
							e.printStackTrace();
						}
						if (!plan2rs.next())
							break;
					}
				}
				contplan.add(curlist);
				plan2rs.close();
			}
			lr = new LoginResponse(cur, plans, contacts, logs, msgs, contplan,
					true);
		}
	}

	public LoginResponse process(DatabaseConnector dc, int empty)
			throws SQLException {
		process(dc);
		return lr;
	}
}
